package com.vm.activity;

import java.util.ArrayList;
import java.util.List;

import com.vm.adapter.FragAdapter;
import com.vm.fragment.DownloadedFragment;
import com.vm.fragment.DownloadingFragment;
import com.vm.myview.NoScrollViewPager;
import com.wwtscapp.apk.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.ImageView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年2月4日 下午2:58:07]
 * @Version: [v1.0]
 * 
 */

public class DownloadManageActivity extends FragmentActivity {
	ImageView imgbtn_search_back;
	List<Fragment> fragments;
	RadioButton btn_downloading;
	RadioButton btn_download_complete;
	private FragAdapter adapter;
	private NoScrollViewPager vp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download_manage_navigation);
		initRbtnListener();
		iniChilds();
	}

	/**
	 * 初始化导航单选按钮监听
	 */
	public void initRbtnListener() {
		// 获取单选按钮group,并设置监听事件
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		btn_downloading = (RadioButton) findViewById(R.id.btn_downloading);
		btn_download_complete = (RadioButton) findViewById(R.id.btn_download_complete);
		btn_download_complete.setOnClickListener(group_listener);
		btn_downloading.setOnClickListener(group_listener);
	}

	/**
	 * 单选按钮监听
	 */
	public OnClickListener group_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_downloading:
				vp.setCurrentItem(0);
				break;
			case R.id.btn_download_complete:
				vp.setCurrentItem(1);
				break;

			default:
				break;
			}
		}

	};

	/**
	 * imgbtn_search_back返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// Log.i(TAG, "imgbtn_search_back返回按钮监听");
			onBackPressed();

		}
	};

	/**
	 * 返回按钮监听(手机自带返回按钮)
	 */
	@Override
	public void onBackPressed() {
		// Log.i(TAG, "返回按钮监听(手机自带返回按钮)");
		super.onBackPressed();
		// System.out.println("按下了back键   onBackPressed()");
	}

	private void iniChilds() {
		fragments = new ArrayList<Fragment>();
		fragments.add(new DownloadingFragment());
		fragments.add(new DownloadedFragment());
		vp = (NoScrollViewPager) findViewById(R.id.viewpager);
		adapter = new FragAdapter(getSupportFragmentManager(), fragments);
		vp.setAdapter(adapter);

		// 设置最大缓存数
		// 默认缓存2个页面,当出现第三个页面会删掉之前缓存中的一个.
		vp.setOffscreenPageLimit(2);
		vp.setCurrentItem(0);
	}
}
